package com.demo.controller_admin;

import java.util.List;
import javax.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.demo.entity.Article;
import com.demo.entity.ResponseResult;
import com.demo.service_admin.impl.ArticleServiceImpl;


@Controller("adminArticleController")
@RequestMapping(value = "//${site.adminUrlPrefix}/article")
public class ArticleController extends BaseController {
	
	@Resource(name="adminArticleService")
	ArticleServiceImpl articleService;

	@GetMapping(value = "/list")
	public String list() {
		return adminTemplatePrefix + "/article_list";
	}
	
	@GetMapping(value = "/list_json")
	public @ResponseBody ResponseResult<List<Article>> list_json(Integer page, Article article) {
		Page<Article> data = articleService.findAll(article, getPageable(page));
		return new ResponseResult<List<Article>>(data.getTotalElements(), data.getContent());
	}
	
	@GetMapping(value = "/detail/{id}")
	public String view(@PathVariable Integer id, Model model) {
		model.addAttribute("entity", articleService.findById(id));
		return "/article_detail";
	}

	@GetMapping(value = "/add")
	public String add(Integer id, Model model) {
		if(id!=null) {
			model.addAttribute("vo", articleService.findById(id));
		}
		return adminTemplatePrefix + "/article_add";
	}
	
	@PostMapping(value = "/save")
	public String save(Article article) {
		articleService.save(article);
		return "redirect:/admin/article/list";
	}
	
	@SuppressWarnings("rawtypes")
	@PostMapping(value = "/isShow")
	public @ResponseBody ResponseResult isShow(Integer id, String isShow) {
		int result = articleService.isShow(id, isShow);
		return ResponseResult.getResponseResult(result);
	}
	
}